package com.heima.user.service.impl;


import com.heima.common.consatnts.user.UserRelationConstants;
import com.heima.common.exception.CustException;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.threadlocal.AppThreadLocalUtils;
import com.heima.model.user.dtos.UserRelationDTO;
import com.heima.model.user.pojos.ApUser;
import com.heima.user.service.ApUserRelationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.stereotype.Service;

/**
 * @author mrchen
 * @date 2022/3/7 15:21
 */
@Service
@Slf4j
public class ApUserRelationServiceImpl implements ApUserRelationService {

    @Autowired
    StringRedisTemplate redisTemplate;

    @Override
    public ResponseResult follow(UserRelationDTO dto) {
        // 1. 参数校验
        //    是否登陆
        ApUser loginUser = AppThreadLocalUtils.getUser();
        if (loginUser == null) {
            CustException.cust(AppHttpCodeEnum.NEED_LOGIN);
        }
        //    operation值必须 0 或  1
        Short operation = dto.getOperation();
        if (operation == null || (operation.intValue() != 0 && operation.intValue() != 1)) {
            CustException.cust(AppHttpCodeEnum.PARAM_INVALID, "操作方式错误");
        }
        ZSetOperations<String, String> zsetOperation = redisTemplate.opsForZSet();
        // 2. 如果是关注请求
        if (operation.intValue() == 0) {
            //    如果当前是关注请求， 查看是否已经关注
            Double score = zsetOperation.score(UserRelationConstants.FOLLOW_LIST + loginUser.getId(), String.valueOf(dto.getAuthorApUserId()));
            if (score != null) {
                CustException.cust(AppHttpCodeEnum.DATA_EXIST, "请勿重复关注");
            }
            //    如果是关注请求， 不能自己关注自己
            if (loginUser.getId().equals(dto.getAuthorApUserId())) {
                CustException.cust(AppHttpCodeEnum.DATA_NOT_ALLOW, "你不能自己关注自己");
            }
            //      follow:loginId   作者id     添加数据
            zsetOperation.add(UserRelationConstants.FOLLOW_LIST + loginUser.getId(), String.valueOf(dto.getAuthorApUserId()), System.currentTimeMillis());
            //     fans : 作者id      loginId   添加数据
            zsetOperation.add(UserRelationConstants.FANS_LIST + dto.getAuthorApUserId(), String.valueOf(loginUser.getId()), System.currentTimeMillis());
        } else {
            // 3.  如果是取消关注请求
            //      follow:loginId   作者id     删除
            zsetOperation.remove(UserRelationConstants.FOLLOW_LIST + loginUser.getId(), String.valueOf(dto.getAuthorApUserId()));
            //     fans : 作者id      loginId   删除
            zsetOperation.remove(UserRelationConstants.FANS_LIST + dto.getAuthorApUserId(), String.valueOf(loginUser.getId()));
        }
        return ResponseResult.okResult();
    }
}